<?php

namespace App\Http\Controllers\Api\Customer;

use App\Service\WdhService;

use Illuminate\Http\Request;

use App\Models\Wx\WxCustomer; //微信会员表

use App\Models\Customer\Customer;  //会员表

use App\Models\ConfigureFunction; //功能配置表

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redis as Redis;

class CustomerAccountController extends Controller
{

    /**
     * 会员积分查询
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function points(Request $request)
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);

        if (empty($session_id)) {
            return response()->json(['code' => 100001, 'message' => '缺少参数：session_id']);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 100002, 'message' => '请登录']);
        }

        //检查会员信息
        $customer = Customer::select('custID', 'cardNo')->find( $pcustID );
        if ( !$customer ) {
            return response()->json(['code' => 100003, 'message' => '会员不存在']);
        }

        //查找粉丝头像,昵称,openID
        $wx_customer = WxCustomer::getCache($pcustID);

        if (!empty($wx_customer)) {
            $openId = $wx_customer['openId'];
            $head_pic = !empty( $wx_customer['headPic'] ) ? $wx_customer['headPic'] :'';
            $nick_name = !empty( $wx_customer['nickName'] )?$wx_customer['nickName']:'';
        } else {
            $openId = '';
            $head_pic = '';
            $nick_name = '';
        }

        //查询账户积分
        $args_data = [
            'cardNo' => $customer['cardNo'],
            'pcustID' => $pcustID,
            'custID' => $customer['custID'],
            'openId' => $openId ? $openId : $customer['custID']
        ];
        $points = WdhService::points( $args_data );

        /**
         * ========================================
         * 查询积分设置
         * ========================================
         */
        $points_show_name = ConfigureFunction::getConfigureField(2, 'show_name', '积分');

        $description = ConfigureFunction::getConfigureField(2, 'points_description', '');

        return response()->json(['code' => 200, 'message' => 'ok','data' => [
            'points' => $points,
            'points_show_name' => $points_show_name,
            'explain' => $description,
            'head_pic' => $head_pic,
            'nick_name' => $nick_name
        ]]);

    }

    /**
     * 查询会员积分明细
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function pointsLogs( Request $request )
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $limit = $request->input('limit');
        $page = $request->input('page');

        if (empty($session_id)) {
            return response()->json(['code' => 100001, 'message' => '缺少参数：session_id']);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 100002, 'message' => '请登录']);
        }

        if ( !ebsig_is_int( $limit )) {
            $limit = 10;
        }

        if (!ebsig_is_int($page)) {
            $page = 1;
        }

        $customer = Customer::select('cardNo')->find( $pcustID );
        if ( !$customer ) {
            return response()->json(['code' => 100003, 'message' => '会员不存在']);
        }

        $return_data = [
            'count' => 0,
            'list' => []
        ];

        //查询积分使用明细
        $args_data = [
            'cardNo' => $customer['cardNo'],
            'page' => $request->input('page'),
            'pageSize' => $limit,
            'limit' => $limit
        ];
        $points_logs = WdhService::pointsLogs( $args_data );
        if ( !empty( $points_logs )) {
            $return_data = [
                'count' => $points_logs['data']['count'],
                'list' => $points_logs['data']['data']
            ];
        }

        return response()->json( ['code' => 200, 'message' => 'ok','data' => $return_data] );

    }

    /**
     * 获取会员余额
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function balance(Request $request)
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);

        if (empty($session_id)) {
            return response()->json(['code' => 100001, 'message' => '缺少参数：session_id']);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 100002, 'message' => '请登录']);
        }

        //检查会员信息
        $customer = Customer::select('custID', 'cardNo')->find( $pcustID );
        if ( !$customer ) {
            return response()->json(['code' => 100003, 'message' => '会员不存在']);
        }

        //查找粉丝头像,昵称,openID
        $wx_customer = WxCustomer::getCache($pcustID);

        if (!empty($wx_customer)) {
            $openId = $wx_customer['openId'];
            $head_pic = !empty( $wx_customer['headPic'] ) ? $wx_customer['headPic'] :'';
            $nick_name = !empty( $wx_customer['nickName'] )?$wx_customer['nickName']:'';
        } else {
            $openId = '';
            $head_pic = '';
            $nick_name = '';
        }

        $args_data = [
            'cardNo' => $customer['cardNo'],
            'pcustID' => $pcustID,
            'custID' => $customer['custID'],
            'openId' => $openId ? $openId : $customer['custID']
        ];

        //查询账户余额
        $balance = WdhService::balance( $args_data );

        //是否开启余额充值
        $is_open_recharge = ConfigureFunction::getConfigureField(3, 'balance_recharge_open');

        //返回参数
        $balance_array = [
            'balance' => $balance,
            'head_pic' => $head_pic,
            'nick_name' => $nick_name,
            'is_open_recharge' => $is_open_recharge  //是否开启余额充值
        ];

        return response()->json( ['code' => 200, 'message' => 'ok','data' => $balance_array] );

    }

    /**
     * 查询会员余额明细
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function balanceLogs( Request $request )
    {

        $session_id = $request->input('session_id', session_id());
        $platform = $request->input('platform', 6);
        $limit = $request->input('limit');
        $page = $request->input('page');

        if (empty($session_id)) {
            return response()->json(['code' => 100001, 'message' => '缺少参数：session_id']);
        }

        $pcustID = get_customer_id($session_id, $platform);
        if (!$pcustID) {
            return response()->json(['code' => 100002, 'message' => '请登录']);
        }

        if ( !ebsig_is_int( $limit )) {
            $limit = 10;
        }

        if (!ebsig_is_int($page)) {
            $page = 1;
        }

        $return_data = [
            'count' => 0,
            'list' => []
        ];

        $customer = Customer::select('cardNo')->find( $pcustID );
        if ( !$customer ) {
            return response()->json(['code' => 100003, 'message' => '会员不存在']);
        }

        $args_data = [
            'cardNo' => $customer->cardNo,
            'page' => $page,
            'pageSize' => $limit,
            'limit' => $limit
        ];

        $balance_logs = WdhService::balanceLogs( $args_data );
        if ( !empty( $balance_logs )) {
            $return_data = [
                'count' => $balance_logs['data']['count'],
                'list' => $balance_logs['data']['data']
            ];
        }

        return response()->json( ['code' => 200, 'message' => 'ok','data' => $return_data] );

    }

}